package com.heima.wemedia.controller.v1;

import com.heima.model.common.constants.wemedia.WemediaConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.NewsAuthDTO;
import com.heima.model.wemedia.dtos.WmNewsDTO;
import com.heima.model.wemedia.dtos.WmNewsPageReqDTO;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.vos.WmNewsVO;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "自媒体文章列表管理")
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Autowired
    WmNewsService wmNewsService;

    @ApiOperation("分页带条件查询自媒体文章列表")
    @PostMapping("/list")
    public ResponseResult<WmNews> findNews(@RequestBody WmNewsPageReqDTO dto){
        return wmNewsService.findNewsByPage(dto);
    }

    @ApiOperation(value = "发表文章",notes = "发表文章，保存草稿，修改文章 共用的方法")
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDTO dto) {
        return wmNewsService.saveOrUpdate(dto);
    }

    @ApiOperation(value = "根据id查询文章信息")
    @GetMapping("/one/{id}")
    public ResponseResult<WmNews> findNewsById(@PathVariable("id") Integer id){
        return wmNewsService.findNewsById(id);
    }
    @ApiOperation(value = "根据id删除文章信息")
    @GetMapping("/del_news/{id}")
    public ResponseResult<WmNews> deleteNewsById(@PathVariable("id") Integer id){
        return wmNewsService.deleteNewsById(id);
    }

    @ApiOperation(value = "文章上下架")
    @PostMapping("/down_or_up")
    public ResponseResult<WmNews> upOrDownEnable(@RequestBody WmNewsDTO wmNewsDTO){
        return wmNewsService.upOrDownNews(wmNewsDTO);
    }
    /*
    * 根据id修改文章内容
    * */
    @ApiOperation("根据id修改自媒体文章")
    @PutMapping("/update")
    public ResponseResult<WmNews> updateWmNews(@RequestBody WmNews wmNews) {
        return wmNewsService.updateNewsById(wmNews);
    }





/*
* 查询文章列表，带作者名称
* */
@PostMapping("/list_vo")
public ResponseResult<WmNewsVO> findList(@RequestBody NewsAuthDTO dto) {
  return wmNewsService.selectAll(dto);
}

    /*
     * 查询文章，带作者名称,
     * */
    @GetMapping("/one_vo/{id}")
    public ResponseResult findWmNewsVo(@PathVariable("id") Integer id) {
   return wmNewsService.selectOne(id);
    }

    /**
     * 文章审核成功
     * @param dto
     * @return
     */
    @ApiOperation("人工审核通过 状态:4")
    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody NewsAuthDTO dto) {
        return wmNewsService.updateStatus(dto,WemediaConstants.WM_NEWS_AUTH_PASS);
    }
    /**
     * 文章审核失败
     * @param dto
     * @return
     */
    @ApiOperation("人工审核失败 状态:2")
    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody NewsAuthDTO dto) {
        return wmNewsService.updateStatus(dto,WemediaConstants.WM_NEWS_AUTH_FAIL);
    }

}
